package com.example.module_base_core.baseview;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.ViewDataBinding;

import com.example.module_base_core.uiuitls.TitleImmerse;
import com.example.module_base_core.utils.UIUtils;
import com.harisucici.jetpackbase.base.HBaseActivity;
import com.harisucici.jetpackbase.base.HBaseViewModel;

import java.util.Date;

/**
 * @Author : 作者
 * @Time : On 2020/12/19 18:39
 * @Page: On com.example.module_base_core.baseview
 * @Description : BaseActivity
 */
public abstract class BaseActivity<V extends ViewDataBinding, VM extends HBaseViewModel> extends HBaseActivity<V, VM> implements View.OnClickListener {
    protected abstract boolean exit();

    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private long lastBackpressTime;
    private static long lastClickTime;

    //双击事件
    public abstract void onMultiClick(View v);

    //设置布局
    public abstract int initCView(Bundle savedInstanceState);

    //绑定binding
    public abstract int initVM();

    /**
     * 设置布局
     *
     * @param savedInstanceState
     * @return
     */
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return initCView(savedInstanceState);
    }

    /**
     * 绑定binding
     *
     * @return
     */
    @Override
    public int initVariableId() {
        return initVM();
    }

    /**
     * "
     * 双击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            onMultiClick(v);
        }
    }

    /**
     * 设置沉浸状态栏下的title
     *
     * @param isImplement
     */
    protected void setTileImmerse(boolean isImplement) {
        if (isImplement) {
            TitleImmerse.assistActivity(findViewById(android.R.id.content));

        }
    }

    @Override
    public void onBackPressed() {
        if (exit()) {
            long pressTime = new Date().getTime();
            if (lastBackpressTime > 0 && (pressTime - lastBackpressTime < 2000)) {
                super.onBackPressed();
                this.finish();
            } else {
                lastBackpressTime = pressTime;
                UIUtils.showToast(this, "再点一次退出应用");
            }
        } else {
            super.onBackPressed();
        }
    }

}
